
var num = 1;
var pageSize = 10;


/**
 * 查询日志记录接口
 * @param pageNum
 * @param pageSize
 * @param callback
 */

function listLogs(pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo?pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("请求日志列表错误！");
        },
        dataType: "json",
    });
}

function listLogsByCond(username, from, to, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo?username="+username+"&fromTime="+ from + "&toTime="+ to +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
            checkLogin(datas);
            addContent(datas);
        },
        error: function(){
            console.log("请求日志列表错误！");
        },
        dataType: "json",
    });
}
/**
 * 按时间区间查询日志接口
 * @param from
 * @param to
 * @param pageNum
 * @param pageSize
 */
function listLogsByTimerange(from, to, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo/timeRange?fromTime="+ from + "&toTime="+ to +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("根据时间区间请求日志列表错误！");
        },
        dataType: "json",
    });
}

/**
 * 按用户名查询日志接口
 * @param username
 * @param pageNum
 * @param pageSize
 */
function listLogsByUser(username, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo/username?username="+ username +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("根据用户名请求日志列表错误！");
        },
        dataType: "json",
    });
}

/**
 * 按用户名和时间区间查询日志接口
 * @param from
 * @param to
 * @param pageNum
 * @param pageSize
 */
function listLogsByUserAndTimerange(username, from, to, pageNum, pageSize){
    $.ajax({
        url: HOST.concat("/logsInfo/username/timeRange?username="+username+"&fromTime="+ from + "&toTime="+ to +"&pageNum=" + pageNum + "&pageSize=" + pageSize + "&token=" + config.token) ,
        type: "GET",
        success: function(datas){
			checkLogin(datas);
        	addContent(datas);
        },
        error: function(){
            console.log("根据用户名和时间区间请求日志列表错误！");
        },
        dataType: "json",
    });
}
function isNull( str ){
    if ( str == "" ) return true;
    // if (str == undefined) return true;
    var regu = "^[ ]+$";
    var re = new RegExp(regu);
    return re.test(str);
}


function getLogsInfoList(num){

        var username=$("#username").val();
         if( username.trim()!="" && !checkLogUsername(username)){
            alert("用户名不能超过16位，且不包含除出下划线和减号以外的特殊字符");
            $("#username").val("");
            return;
         }
        var from=$("#from").val();
        var to=$("#to").val();
        if(!isNull(from)&&!isNull(to)) {
            if (compareFromAndTo(from,to)){
                from = from.replace(/-/g, "/") + " 00:00:00";
                to = to.replace(/-/g, "/") + " 23:59:59";
                listLogsByCond(username, from, to, num, pageSize);
            }else {
                alert("时间区间先后存在问题!");
            }
        }else if(!isNull(from)){
            from = from.replace(/-/g, "/") + " 00:00:00";
            listLogsByCond(username, from, to, num, pageSize);
        }else if(!isNull(to)){
            to = to.replace(/-/g, "/") + " 23:59:59";
            listLogsByCond(username, from, to, num, pageSize);
        }else {
            listLogsByCond(username, from, to, num, pageSize);
        }

        /*//若按用户名和时间区间查询
        if(!isNull(username) && !isNull(from) && !isNull(to) ){
            console.log("按用户名和时间区间查询");
            if(compareFromAndTo(from,to)){
                from=from.replace(/-/g,"/") + " 00:00:00";
                to=to.replace(/-/g,"/") + " 00:00:00";
                listLogsByUserAndTimerange(username, from, to, num, pageSize)
            }else {
                alert("时间区间先后存在问题!");
            }

        }
        //若仅按时间区间查询
        else if(!isNull(from) && !isNull(to)){
            console.log("按时间区间查询");
            if(compareFromAndTo(from,to)){
                from=from.replace(/-/g,"/") + " 00:00:00";
                to=to.replace(/-/g,"/") + " 00:00:00";
                listLogsByTimerange(from, to, num, pageSize);
            }else {
                alert("时间区间先后存在问题!");
            }

        }
        //若仅按用户名查询
        else if(!isNull(username)){
            console.log("按用户名查询");
            listLogsByUser(username, num, pageSize);
        }
        else{
            console.log("默认查询");
            listLogs(num, pageSize);
        }*/
}
function compareFromAndTo(from,to) {
    var fromTime = from.toString().split("-")
    var toTime = to.toString().split("-");
    if(fromTime[0]>toTime[0]||fromTime[1]>toTime[1]||fromTime[2]>toTime[2]){
        return false;
    }else {
        return true;
    }
}

function addContent(res){
        //查询成功
        console.log(res);
        if(res.meta.state==="success") {
            var list = res.data;

				console.log("有查询记录");
				$("#tbody").empty();
				$("#pageNum").text(num);
				for (var p in list) {
				    var item = list[p];//单条数据
				    var createTime = item["createTime"];
				    var username = item["username"];
				    var operateInfo = item["operateInfo"];
				    //console.log(operateInfo);
				    var result = item["result"];
				
				    $("#tbody").append("<div class=\"op-data tr clearfix border-bottom-none\">" +
				        "                        <div class=\"td w25\">" + createTime + "</div>" +
				        "                        <div class=\"td w25\">" + username + "</div>" +
				        "                        <div class=\"td w25\">" + operateInfo + "</div>" +
				        "                        <div class=\"td w25\">" + result + "</div>" +
				        "               </div>");
				}

            
        }
        //查询失败
        else if(res.meta.state=="failed"){
            if(num==1){
                $("#tbody").empty();
                alert("无查询结果!");
            }else {
                alert("已经是最后一页!");
                num--;
            }
        }
}



function logsLastPage(){
	if(num<=1){
		num = 1;
		alert("已经是第一页!");
		$("#pageNum").text(num);
	}else{
		num--;
		getLogsInfoList(num);
	}
}

function logsNextPage(){
		num++;
		getLogsInfoList(num);
}
function logsFirstPage(){
	num = 1;
	$("#pageNum").text(num);
	getLogsInfoList(num);
}
$("#search").click(function(){
	num = 1;
	$("#pageNum").text(num);
	getLogsInfoList(num);
});
getLogsInfoList(1);
